// Copyright 2020 morgine.com. All rights reserved.

package handlers

import (
	"errors"
	"gitee.com/tugoer/psys/container"
	"gitee.com/tugoer/psys/models"
	"gitee.com/tugoer/psys/utils"
	"github.com/gin-gonic/gin"
)

func GetGame(ctx *gin.Context) {
	id, ok := ctx.Params.Get("id")
	if ok {
		game, err := container.Game.Get(id)
		if err != nil {
			ctx.AbortWithError(500, err)
		} else {
			ctx.JSON(200, gin.H{"game": game})
		}
	} else {
		ctx.AbortWithError(500, errors.New("id is not provided"))
	}
}

type getResult struct {
	ID     string `form:"id"`
	Option string `form:"option"`
	Acid   string `form:"acid"`
	Ext    string `form:"ext"`
}

func GetGameResult(ctx *gin.Context) {
	p := &getResult{}
	err := ctx.Bind(p)
	if err != nil {
		ctx.AbortWithError(500, err)
	} else {
		var res *models.Result
		res, err = container.Result.Get(p.ID, p.Option, p.Acid, p.Ext)
		if err != nil {
			ctx.AbortWithError(500, err)
		} else {
			ctx.JSON(200, gin.H{"result": res})
		}
	}
}

type getSubjects struct {
	Order utils.Order `form:"order" desc:"排序规则: utime-按最近更新排序, view-按热度排序" required:""`
	Page  string      `form:"page" desc:"获取第几页数据" required:""`
}

func GetSubjects(ctx *gin.Context) {
	p := &getSubjects{}
	if err := ctx.Bind(p); err != nil {
		ctx.AbortWithError(500, err)
	} else {
		subjects, err := container.Subjects.Get(p.Order, p.Page)
		if err != nil {
			ctx.AbortWithError(500, err)
		} else {
			ctx.JSON(200, gin.H{"subjects": subjects})
		}
	}
}
